package org.swing.utility.input.util;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.RootPaneContainer;

/**
 * Provides static utility methods for JFrames.
 */
public final class FrameUtil {
	/**
	 * No instances allowed. Just use the static methods.
	 */
	private FrameUtil() {
		super();
	}

	/**
	 * Sets the button that is "activated" when the escape key is used. A bit
	 * like SwingUtilities.getRootPane(this).setDefaultButton(myButton);
	 * 
	 * @thatisjava.com.param jFrame The JFrame or JDialog for which the escape
	 *                       key button should be set.
	 * @thatisjava.com.param button The button which should be clicked
	 *                       automatically when the escape key is pressed.
	 * */
	public static void setEscapeKeyButton(final RootPaneContainer jFrame,
			final JButton button) {
		addEscapeKeyAction(jFrame, new AbstractAction() {
			public void actionPerformed(final ActionEvent evt) {
				button.doClick();
			}
		});
	}

	/**
	 * Adds an action to the jframe's action map that is triggered by the Escape
	 * key. (If you just want to simulate a button click when the escape key is
	 * pressed then use setEscapeKeyButton.) A bit like
	 * SwingUtilities.getRootPane(this).setDefaultButton(myButton);
	 * 
	 * @thatisjava.com.param jFrame The JFrame or JDialog for which the escape
	 *                       key action should be set.
	 * @thatisjava.com.param action The action that is executred when the escape
	 *                       key us pressed,
	 * */
	public static void addEscapeKeyAction(final RootPaneContainer jFrame,
			final Action action) {
		String actionKey = "escapeAction";
		KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		// using getRootPane rather than getContentPane, content pane doesn't
		// work in all
		// scenarios
		InputMap inputMap = (jFrame.getRootPane())
				.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		inputMap.put(escape, actionKey);
		ActionMap actionMap = (jFrame.getRootPane()).getActionMap();
		actionMap.put(actionKey, action);
	}

	/**
	 * Finds the escape action for the given frame or dialog if one was added
	 * 
	 * @thatisjava.com.param jFrame The JFrame or JDialog for which the escape
	 *                       key action should be set.
	 * */
	public static Action getEscapeKeyAction(final RootPaneContainer jFrame) {
		InputMap inputMap = (jFrame.getRootPane())
				.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,
				0);
		ActionMap actionMap = (jFrame.getRootPane()).getActionMap();
		Object actionKey = inputMap.get(escapeKeyStroke);
		Action escapeAction = null;
		if (actionKey != null) {
			escapeAction = actionMap.get(actionKey);
		}
		return escapeAction;
	}
}
